$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/order/list',
        datatype: "json",
        colModel: [
			{ label: 'id', name: 'id', index: 'id', width: 80 },
			{ label: 'uid', name: 'userId', index: 'userId', width: 80 },
			{ label: '手机账号', name: 'mobile', index: 'mobile', width: 120 },
			{ label: '订单类型', name: 'type', index: 'type', width: 80 , formatter: function(value, options, row){
					if(!value && value!=0){
						return '未知'
					}
				var g = {
					1:"CNY订单",
                    2:"USDT订单"
				}
					if(!g[value]){
						return '未知'
					}
                return g[value];
				}
			},
			{ label: '状态', name: 'active', index: 'active', width: 80 ,formatter: function (value, options, row) {
				if(value == 0){
					return "<span style='color: #008d4c'>已停止</span>";
				} else if(value == 1){
					return "<span style='color: #af0000'>释放中</span>";
				} else {
					return "<span style='color: #af0000'>未知</span>";
				}
			}},
			{ label: '商品名称', name: 'proName', index: 'pro_name', width: 160 },
			{ label: '单价cny', name: 'priceCny', index: 'price_cny', width: 120 },
			{ label: '单价usdt', name: 'priceUsdt', index: 'price_usdt', width: 80 },
			{ label: '数量', name: 'quantity', index: 'quantity', width: 80 },
			{ label: '总价cny', name: 'amountCny', index: 'amount_cny', width: 100 },
			{ label: '总价usdt', name: 'amountUsdt', index: 'amount_usdt', width: 100 },
			{ label: '收益率', name: 'rate', index: 'rate', width: 100 },
			{ label: '收益金额', name: 'income', index: 'income', width: 100 },
			{ label: '总产量', name: 'totalIncome', index: 'total_income', width: 80 },
			{ label: '剩余产出', name: 'leftIncome', index: 'left_income', width: 80 },
			{ label: '产出总天数', name: 'totalTimes', index: 'total_times', width: 100 },
			{ label: '剩余发放天数', name: 'leftTimes', index: 'left_times', width: 100 },
			// { label: '认证状态', name: 'status', index: 'status', width: 80 , formatter: function(value, options, row){
			// 		if(!value && value!=0){
			// 			return '未知'
			// 		}
			// 	var g = {
			// 			0 :"未结算", 1: "已结算",2: "待结算"
			// 		}
			// 		if(!g[value]){
			// 			return '未知'
			// 		}
			// 		return g[value];
			// 	}
			// },
			{ label: '创建时间', name: 'createDate', index: 'create_date', width: 150 }
		],
		viewrecords: true,
		height: 385,
		rowNum: 10,
		rowList : [10,30,50],
		rownumbers: true,
		rownumWidth: 25,
		shrinkToFit:false,
		autoScroll: true,
		autowidth:true,
		multiselect: true,
		pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
		beforeProcessing: function(data, status, xhr) {
			// 假设我们检查名为"errorField"的字段
			if (data.code !== 0) {
				// 如果满足条件，则显示错误消息
				alert(data.msg);
				// 停止处理，不显示数据
				return false;
			}
			// 如果没有错误，继续处理数据
			return true;
		},
        gridComplete:function(){
        	//隐藏grid底部滚动条
			$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "show" });
        }
    });

	$("#createTime").datepicker({
		controlType: 'select',
		showOtherMonths: true,
		selectOtherMonths: true,
		maxDate: new Date()
	});
	$("#endTime").datepicker({
		controlType: 'select',
		showOtherMonths: true,
		selectOtherMonths: true,
		maxDate: new Date(),
	});
});

function showImg(val,tit){
	$("#showImg").attr("src",val);
	layer.open({
		type: 1,
		skin: 'layui-layer-molv',
		title: tit,
		area: ['650px', '500px'],
		shade: 0,
		shadeClose: false,
		content: jQuery("#Img"),
		btn: ['确定'],
		btn1: function (index) {
			layer.close(index);
		}
	});


}

var vm = new Vue({
	el:'#rrapp',
	data:{
        q:{
			keyWord: '',
			type: '',
			status: ''
        },
		showList: true,
		title: null,
		auth: {},
		dict: {}
	},
	methods: {
		clear_s: function () {
			$("#createTime").val('');
		},
		clear_e: function () {
			$("#endTime").val('');
		},
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.dict = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		kill: function (event) {
			var id = getSelectedOne();
			if(id == null){
				return ;
			}
			confirm('确定暂停/开启收益？', function(){
				$.ajax({
					type: "POST",
					url: baseURL + "sys/order/pass/off",
					contentType: "application/json",
					data: JSON.stringify(id),
					success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		downloadEx: function (event) {
			var createTime =   $("#createTime").val();
			var endTime =  $("#endTime").val();
			if (createTime && endTime && createTime.length > 0 && endTime.length > 0) {
				var startTmp = createTime.split("-");
				var endTmp = endTime.split("-");
				var sd = new Date(startTmp[0], startTmp[1], startTmp[2]);
				var ed = new Date(endTmp[0], endTmp[1], endTmp[2]);
				if (sd.getTime() > ed.getTime()) {
					alert("开始日期不能大于结束日期");
					return ;
				}
			}
			if (createTime && createTime.length > 0) {
				createTime += " 00:00:00";
			}
			if (endTime && endTime.length > 0) {
				endTime += " 23:59:59";
			}

			const url= "sys/order/export?da="+new Date().getTime();
			var formData = `&startTime=${createTime}&endTime=${endTime}&keyWord=${vm.q.keyWord}&type=${vm.q.type}&status=${vm.q.status}`
			window.location.href = baseURL + url + formData;
		},
		saveOrUpdate: function (event) {
			const url= "sys/order/edit";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.dict),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/order/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/order/"+id, function(r){
                vm.dict = r.data;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var createTime =   $("#createTime").val();
			var endTime =  $("#endTime").val();
			if (createTime && endTime && createTime.length > 0 && endTime.length > 0) {
				var startTmp = createTime.split("-");
				var endTmp = endTime.split("-");
				var sd = new Date(startTmp[0], startTmp[1], startTmp[2]);
				var ed = new Date(endTmp[0], endTmp[1], endTmp[2]);
				if (sd.getTime() > ed.getTime()) {
					alert("开始日期不能大于结束日期");
					return ;
				}
			}
			if (createTime && createTime.length > 0) {
				createTime += " 00:00:00";
			}
			if (endTime && endTime.length > 0) {
				endTime += " 23:59:59";
			}
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
					"startTime":createTime,
					"endTime":endTime,
					'keyWord': vm.q.keyWord,"type":vm.q.type,"proName":vm.q.proName,"status":vm.q.status},
                page:1
            }).trigger("reloadGrid");
		}
	}
});